
#include "InputManager.h"


InputManager * InputManager::instance = NULL;


InputManager* InputManager::getInstance()
{
	if (!instance)
		instance = new InputManager();
	return instance;
}


InputManager::InputManager() {
	quitGame = false;
}


bool InputManager::Update()
{
	if (SDL_PollEvent(&event))
	{

		//Zera os vetores
		for (int i = 0; i < N_KEYS; i++)
			keyDown[i] = keyUp[i] = false;

		for (int i = 0; i < N_MOUSE_BUTTONS; i++)
			mouseDown[i] = mouseUp[i] = false;

		//Atualiza o estado do mouse e teclado
		mouse = SDL_GetMouseState(&mouseX, &mouseY);
		teclado = SDL_GetKeyState(NULL);

		//Verifica tipo do evento e toma a acao
		switch (event.type)
		{
		case SDL_KEYDOWN:
			keyDown[event.key.keysym.sym] = true;
			break;
		case SDL_KEYUP:
			keyUp[event.key.keysym.sym] = true;
			break;
		case SDL_MOUSEMOTION:
			mouseX = event.motion.x;
			mouseY = event.motion.y;
			break;
		case SDL_MOUSEBUTTONDOWN:
			mouseDown[event.button.button] = true;
			break;
		case SDL_MOUSEBUTTONUP:
			mouseUp[event.button.button] = true;
			break;
		case SDL_QUIT:
			quitGame = true;
			break;
		}

		return true;
	} else {
		return false;
	}
}


bool InputManager::isKeyDown(int key)
{
	return event.key.keysym.sym == key;
}


bool InputManager::isKeyUp(int key)
{
	return event.key.keysym.sym == key;
}


bool InputManager::isMouseDown(int button)
{
	return event.button.button == button;
}


bool InputManager::isMouseUp(int button)
{
	return event.button.button == button;
}


bool InputManager::isKeyPressed(int key)
{
	return keyDown[key];
}


bool InputManager::isMousePressed(Uint8 button)
{
	return mouseDown[button];
}


int InputManager::mousePosX()
{
	return mouseX;
}


int InputManager::mousePosY()
{
	return mouseY;
}


bool InputManager::isMouseInside(SDL_Rect rect)
{
	if ((mouseX >= rect.x) && (mouseX < rect.x + rect.w) && (mouseY >= rect.y) && (mouseY < rect.y + rect.h))
		return	true;
	else
		return false;
}


bool InputManager::QuitGame()
{
	return quitGame;
}


Uint8 InputManager::returnEventType() {
	return event.type;
}


